<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-sets.html" />
<link rel="prev" href="module-bisect.html" />
<link rel="parent" href="datatypes.html" />
<link rel="next" href="module-sets.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>5.6 array -- Efficient arrays of numeric values</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.5.1 Examples"
  href="bisect-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5. data Types"
  href="datatypes.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.7 sets  "
  href="module-sets.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="bisect-example.html">5.5.1 Examples</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="datatypes.html">5. Data Types</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-sets.html">5.7 sets  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION007600000000000000000">
5.6 <tt class="module">array</tt> --
         Efficient arrays of numeric values</a>
</h1>

<p>
<a name="module-array"></a>

<p>
This module defines an object type which can efficiently represent
an array of basic values: characters, integers, floating point
numbers.  Arrays<a id='l2h-785' xml:id='l2h-785'></a> are sequence types and behave very much
like lists, except that the type of objects stored in them is
constrained.  The type is specified at object creation time by using a
<i class="dfn">type code</i>, which is a single character.  The following type
codes are defined:

<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="center">Type code</th>
      <th class="left"  >C Type</th>
      <th class="left"  >Python Type</th>
      <th class="center">Minimum size in bytes</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="center" valign="baseline"><code>'c'</code></td>
        <td class="left"  >char</td>
        <td class="left"  >character</td>
        <td class="center">1</td></tr>
    <tr><td class="center" valign="baseline"><code>'b'</code></td>
        <td class="left"  >signed char</td>
        <td class="left"  >int</td>
        <td class="center">1</td></tr>
    <tr><td class="center" valign="baseline"><code>'B'</code></td>
        <td class="left"  >unsigned char</td>
        <td class="left"  >int</td>
        <td class="center">1</td></tr>
    <tr><td class="center" valign="baseline"><code>'u'</code></td>
        <td class="left"  >Py_UNICODE</td>
        <td class="left"  >Unicode character</td>
        <td class="center">2</td></tr>
    <tr><td class="center" valign="baseline"><code>'h'</code></td>
        <td class="left"  >signed short</td>
        <td class="left"  >int</td>
        <td class="center">2</td></tr>
    <tr><td class="center" valign="baseline"><code>'H'</code></td>
        <td class="left"  >unsigned short</td>
        <td class="left"  >int</td>
        <td class="center">2</td></tr>
    <tr><td class="center" valign="baseline"><code>'i'</code></td>
        <td class="left"  >signed int</td>
        <td class="left"  >int</td>
        <td class="center">2</td></tr>
    <tr><td class="center" valign="baseline"><code>'I'</code></td>
        <td class="left"  >unsigned int</td>
        <td class="left"  >long</td>
        <td class="center">2</td></tr>
    <tr><td class="center" valign="baseline"><code>'l'</code></td>
        <td class="left"  >signed long</td>
        <td class="left"  >int</td>
        <td class="center">4</td></tr>
    <tr><td class="center" valign="baseline"><code>'L'</code></td>
        <td class="left"  >unsigned long</td>
        <td class="left"  >long</td>
        <td class="center">4</td></tr>
    <tr><td class="center" valign="baseline"><code>'f'</code></td>
        <td class="left"  >float</td>
        <td class="left"  >float</td>
        <td class="center">4</td></tr>
    <tr><td class="center" valign="baseline"><code>'d'</code></td>
        <td class="left"  >double</td>
        <td class="left"  >float</td>
        <td class="center">8</td></tr></tbody>
</table></div>

<p>
The actual representation of values is determined by the machine
architecture (strictly speaking, by the C implementation).  The actual
size can be accessed through the <tt class="member">itemsize</tt> attribute.  The values
stored  for <code>'L'</code> and <code>'I'</code> items will be represented as
Python long integers when retrieved, because Python's plain integer
type cannot represent the full range of C's unsigned (long) integers.

<p>
The module defines the following type:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-761' xml:id='l2h-761' class="function">array</tt></b>(</nobr></td>
  <td><var>typecode</var><big>[</big><var>, initializer</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a new array whose items are restricted by <var>typecode</var>,
and initialized from the optional <var>initializer</var> value, which
must be a list, string, or iterable over elements of the
appropriate type.

<span class="versionnote">Changed in version 2.4:
Formerly, only lists or strings were accepted.</span>

If given a list or string, the initializer is passed to the
new array's <tt class="method">fromlist()</tt>, <tt class="method">fromstring()</tt>, or
<tt class="method">fromunicode()</tt> method (see below) to add initial items to
the array.  Otherwise, the iterable initializer is passed to the
<tt class="method">extend()</tt> method.
</dl>

<p>
<dl><dt><b><tt id='l2h-762' xml:id='l2h-762'>ArrayType</tt></b></dt>
<dd>
Obsolete alias for <tt class="function">array</tt>.
</dd></dl>

<p>
Array objects support the ordinary sequence operations of
indexing, slicing, concatenation, and multiplication.  When using
slice assignment, the assigned value must be an array object with the
same type code; in all other cases, <tt class="exception">TypeError</tt> is raised.
Array objects also implement the buffer interface, and may be used
wherever buffer objects are supported.

<p>
The following data items and methods are also supported:

<p>
<dl><dt><b><tt id='l2h-763' xml:id='l2h-763' class="member">typecode</tt></b></dt>
<dd>
The typecode character used to create the array.
</dl>

<p>
<dl><dt><b><tt id='l2h-764' xml:id='l2h-764' class="member">itemsize</tt></b></dt>
<dd>
The length in bytes of one array item in the internal representation.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-765' xml:id='l2h-765' class="method">append</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Append a new item with value <var>x</var> to the end of the array.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-766' xml:id='l2h-766' class="method">buffer_info</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return a tuple <code>(<var>address</var>, <var>length</var>)</code> giving the current
memory address and the length in elements of the buffer used to hold
array's contents.  The size of the memory buffer in bytes can be
computed as <code><var>array</var>.buffer_info()[1] *
<var>array</var>.itemsize</code>.  This is occasionally useful when working with
low-level (and inherently unsafe) I/O interfaces that require memory
addresses, such as certain <tt class="cfunction">ioctl()</tt> operations.  The
returned numbers are valid as long as the array exists and no
length-changing operations are applied to it.

<p>
<span class="note"><b class="label">Note:</b>
When using array objects from code written in C or
C++ (the only way to effectively make use of this information), it
makes more sense to use the buffer interface supported by array
objects.  This method is maintained for backward compatibility and
should be avoided in new code.  The buffer interface is documented in
the <em class="citetitle"><a
 href="../api/newTypes.html"
 title="Python/C API Reference Manual"
 >Python/C API Reference Manual</a></em>.</span>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-767' xml:id='l2h-767' class="method">byteswap</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
``Byteswap'' all items of the array.  This is only supported for
values which are 1, 2, 4, or 8 bytes in size; for other types of
values, <tt class="exception">RuntimeError</tt> is raised.  It is useful when reading
data from a file written on a machine with a different byte order.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-768' xml:id='l2h-768' class="method">count</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the number of occurrences of <var>x</var> in the array.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-769' xml:id='l2h-769' class="method">extend</tt></b>(</nobr></td>
  <td><var>iterable</var>)</td></tr></table></dt>
<dd>
Append items from <var>iterable</var> to the end of the array.  If
<var>iterable</var> is another array, it must have <em>exactly</em> the same
type code; if not, <tt class="exception">TypeError</tt> will be raised.  If
<var>iterable</var> is not an array, it must be iterable and its
elements must be the right type to be appended to the array.

<span class="versionnote">Changed in version 2.4:
Formerly, the argument could only be another array.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-770' xml:id='l2h-770' class="method">fromfile</tt></b>(</nobr></td>
  <td><var>f, n</var>)</td></tr></table></dt>
<dd>
Read <var>n</var> items (as machine values) from the file object <var>f</var>
and append them to the end of the array.  If less than <var>n</var> items
are available, <tt class="exception">EOFError</tt> is raised, but the items that were
available are still inserted into the array.  <var>f</var> must be a real
built-in file object; something else with a <tt class="method">read()</tt> method won't
do.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-771' xml:id='l2h-771' class="method">fromlist</tt></b>(</nobr></td>
  <td><var>list</var>)</td></tr></table></dt>
<dd>
Append items from the list.  This is equivalent to
"<tt class="samp">for x in <var>list</var>: a.append(x)</tt>"except that if there is a type error, the array is unchanged.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-772' xml:id='l2h-772' class="method">fromstring</tt></b>(</nobr></td>
  <td><var>s</var>)</td></tr></table></dt>
<dd>
Appends items from the string, interpreting the string as an
array of machine values (as if it had been read from a
file using the <tt class="method">fromfile()</tt> method).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-773' xml:id='l2h-773' class="method">fromunicode</tt></b>(</nobr></td>
  <td><var>s</var>)</td></tr></table></dt>
<dd>
Extends this array with data from the given unicode string.  The array
must be a type <code>'u'</code> array; otherwise a <tt class="exception">ValueError</tt>
is raised.  Use "<tt class="samp">array.fromstring(ustr.decode(enc))</tt>" to
append Unicode data to an array of some other type.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-774' xml:id='l2h-774' class="method">index</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the smallest <var>i</var> such that <var>i</var> is the index of
the first occurrence of <var>x</var> in the array.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-775' xml:id='l2h-775' class="method">insert</tt></b>(</nobr></td>
  <td><var>i, x</var>)</td></tr></table></dt>
<dd>
Insert a new item with value <var>x</var> in the array before position
<var>i</var>. Negative values are treated as being relative to the end
of the array.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-776' xml:id='l2h-776' class="method">pop</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>i</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Removes the item with the index <var>i</var> from the array and returns
it. The optional argument defaults to <code>-1</code>, so that by default
the last item is removed and returned.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-777' xml:id='l2h-777' class="method">read</tt></b>(</nobr></td>
  <td><var>f, n</var>)</td></tr></table></dt>
<dd>
<div class="versionnote"><b>Deprecated since release 1.5.1.</b>
Use the <tt class="method">fromfile()</tt> method.</div><p></p>
Read <var>n</var> items (as machine values) from the file object <var>f</var>
and append them to the end of the array.  If less than <var>n</var> items
are available, <tt class="exception">EOFError</tt> is raised, but the items that were
available are still inserted into the array.  <var>f</var> must be a real
built-in file object; something else with a <tt class="method">read()</tt> method won't
do.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-778' xml:id='l2h-778' class="method">remove</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Remove the first occurrence of <var>x</var> from the array.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-779' xml:id='l2h-779' class="method">reverse</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Reverse the order of the items in the array.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-780' xml:id='l2h-780' class="method">tofile</tt></b>(</nobr></td>
  <td><var>f</var>)</td></tr></table></dt>
<dd>
Write all items (as machine values) to the file object <var>f</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-781' xml:id='l2h-781' class="method">tolist</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Convert the array to an ordinary list with the same items.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-782' xml:id='l2h-782' class="method">tostring</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Convert the array to an array of machine values and return the
string representation (the same sequence of bytes that would
be written to a file by the <tt class="method">tofile()</tt> method.)
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-783' xml:id='l2h-783' class="method">tounicode</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Convert the array to a unicode string.  The array must be
a type <code>'u'</code> array; otherwise a <tt class="exception">ValueError</tt> is raised.
Use "<tt class="samp">array.tostring().decode(enc)</tt>" to obtain a unicode string
from an array of some other type.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-784' xml:id='l2h-784' class="method">write</tt></b>(</nobr></td>
  <td><var>f</var>)</td></tr></table></dt>
<dd>
<div class="versionnote"><b>Deprecated since release 1.5.1.</b>
Use the <tt class="method">tofile()</tt> method.</div><p></p>
Write all items (as machine values) to the file object <var>f</var>.
</dl>

<p>
When an array object is printed or converted to a string, it is
represented as <code>array(<var>typecode</var>, <var>initializer</var>)</code>.  The
<var>initializer</var> is omitted if the array is empty, otherwise it is a
string if the <var>typecode</var> is <code>'c'</code>, otherwise it is a list of
numbers.  The string is guaranteed to be able to be converted back to
an array with the same type and value using reverse quotes
(<code>``</code>), so long as the <tt class="function">array()</tt> function has been
imported using <code>from array import array</code>.  Examples:

<p>
<div class="verbatim"><pre>
array('l')
array('c', 'hello world')
array('u', u'hello \textbackslash u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])
</pre></div>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-struct.html">struct</a></tt>:</b>
    <dd>Packing and unpacking of heterogeneous binary data.
  </dl>
  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-xdrlib.html">xdrlib</a></tt>:</b>
    <dd>Packing and unpacking of External Data
                     Representation (XDR) data as used in some remote
                     procedure call systems.
  </dl>
  <dl compact="compact" class="seetitle">
    <dt><em class="citetitle"><a href="http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm"
        >The
           Numerical Python Manual</a></em></dt>
    <dd>The Numeric Python extension
           (NumPy) defines another array type; see
           <a class="url" href="http://numpy.sourceforge.net/">http://numpy.sourceforge.net/</a> for further information
           about Numerical Python.  (A PDF version of the NumPy manual
           is available at
           <a class="url" href="http://numpy.sourceforge.net/numdoc/numdoc.pdf">http://numpy.sourceforge.net/numdoc/numdoc.pdf</a>).</dd>
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.5.1 Examples"
  href="bisect-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5. data Types"
  href="datatypes.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.7 sets  "
  href="module-sets.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="bisect-example.html">5.5.1 Examples</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="datatypes.html">5. Data Types</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-sets.html">5.7 sets  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
